package net;

import java.sql.Time;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

import androidui.com.sj.latte.app.ConfigType;
import androidui.com.sj.latte.app.Latte;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by wzp on 2019/2/24 0024.
 * <p>
 * 描述  ：todo
 */

public class RestCreator {

    private static final class ParamHolder{
        public static final WeakHashMap<String ,Object> PARAMS= new WeakHashMap<>();
    }

     public static WeakHashMap<String ,Object> getParams(){
        return ParamHolder.PARAMS;
    }

     public static RestService getRestService(){
         return RestServiceHolder.REST_SERVICE;
     }


    private static final class RetrofitHolder{
        private static final String BASE_URL =
                (String) Latte.getConfigurations().get(ConfigType.API_HOST.name());
        private static final Retrofit RETROFIT_CLIENT =  new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(OkhttpHolder.OK_HTTP_CLIENT)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

    }

    //OKhttp  惰性 初始化
    private static final class OkhttpHolder{
        private static final int TIME_OUT =  60;
        private static final OkHttpClient OK_HTTP_CLIENT
                =  new OkHttpClient.Builder()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .build();
    }

    //获取retrofit service
    private static final class RestServiceHolder{

        private static final RestService REST_SERVICE =
                RetrofitHolder.RETROFIT_CLIENT.create(RestService.class);


    }
}
